package book.hadoop.ch04;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.MapFile;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;
/**
 * 参数：hdfs://hadoop.main:9000/sequence-file-test
 * @author conkeyn
 *
 */
public class MapFIleWriterDemo {

	private static final String[] DATA = { "One,two,buckle my shoe", "Three, four, shut the door", "Five, Six, pick up sticks", "Seven,eight,lay them straight", "Nine,ten, a big fat hen" };

	public static void main(String[] args) throws IOException, URISyntaxException {
		String uri = args[0];
		Configuration config = new Configuration();
		FileSystem fs = FileSystem.get(new URI(uri), config);
		Path path = new Path(uri);
		IntWritable key = new IntWritable();
		Text value = new Text();
		MapFile.Writer writer = null;
		try {
			writer = new MapFile.Writer(config, fs, uri, key.getClass(), value.getClass());
			writer.setIndexInterval(256);//值越大，占用内存越小，但是搜索时间反而变久。
			for (int i = 0; i < 1024; i++) {
				key.set(i+ 1);
				value.set(DATA[i % DATA.length]);
				writer.append(key, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeStream(writer);
		}
	}

}
